/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * V1ModColectAcept.java
 *
 * Created on 14-may-2012, 12:42:10
 */
package practtica4;

import BBDD.GestorBBDD;
import DTO.Alias;
import DTO.Colectivo;
import DTO.Fichero;
import DTO.Personaje;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario_local
 */
public class V1ModColectAcept extends javax.swing.JFrame {

    /** Creates new form V1ModColectAcept */
    Colectivo colectivo;
    GestorBBDD gestor=new GestorBBDD();
    public V1ModColectAcept() {
        initComponents();
    }

    V1ModColectAcept(Colectivo col) {
        this.colectivo=col;
        initComponents();
        cargarEtiquetas();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDesc = new javax.swing.JLabel();
        botonAceptar = new javax.swing.JButton();
        labelNombre = new javax.swing.JLabel();
        botonDelete = new javax.swing.JButton();
        botonNombre = new javax.swing.JTextField();
        botonDescr3 = new javax.swing.JTextField();
        labelId = new javax.swing.JLabel();
        botonId = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        botonfich = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelDesc.setText("descripción");

        botonAceptar.setText("Aceptar");
        botonAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonAceptarMouseClicked(evt);
            }
        });

        labelNombre.setText("nombre");

        botonDelete.setText("Delete");
        botonDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonDeleteMouseClicked(evt);
            }
        });

        botonDescr3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDescr3ActionPerformed(evt);
            }
        });

        labelId.setText("id");

        jLabel1.setText("Fichero");

        botonfich.setColumns(20);
        botonfich.setRows(5);
        jScrollPane1.setViewportView(botonfich);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelId)
                            .addComponent(labelDesc)
                            .addComponent(labelNombre)
                            .addComponent(jLabel1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(115, 115, 115)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(botonNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                        .addComponent(botonId, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                                    .addComponent(botonDescr3, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(botonDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 233, Short.MAX_VALUE)
                        .addComponent(botonAceptar)))
                .addGap(40, 40, 40))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelId)
                    .addComponent(botonId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(botonNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDesc)
                    .addComponent(botonDescr3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonDelete))
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void botonDescr3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDescr3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonDescr3ActionPerformed

    private void botonDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonDeleteMouseClicked
        // TODO add your handling code here:
         try {
            gestor.eliminarColectivo(colectivo);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
        v1.setVisible(true);
            this.dispose();
    }//GEN-LAST:event_botonDeleteMouseClicked

    private void botonAceptarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonAceptarMouseClicked
        // TODO add your handling code here:
        
        //colectivo.setId(Integer.parseInt(this.botonId.getText()));
        colectivo.setNombre(this.botonNombre.getText());
        colectivo.setDescripcion(this.botonDescr3.getText());
        
        String fich=this.botonfich.getText();
        ArrayList<Fichero> fi=colectivo.getFich();
        int fin=0;
        int ini=0;
        int i=0;
        while(fin<fich.length()){
            ini=fin+1;
            fin= fich.indexOf('\n', ini);
            fi.get(i).setNombre(fich.substring(ini, ini));
            i++;
        }
        colectivo.setFich(fi);
         try {
            gestor.guardarColectivo(colectivo);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
        v1.setVisible(true);
            this.dispose();
    }//GEN-LAST:event_botonAceptarMouseClicked

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonDelete;
    private javax.swing.JTextField botonDescr3;
    private javax.swing.JTextField botonId;
    private javax.swing.JTextField botonNombre;
    private javax.swing.JTextArea botonfich;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDesc;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelNombre;
    // End of variables declaration//GEN-END:variables

    private void cargarEtiquetas() {
        String id=colectivo.getId()+"";
        this.botonId.setText(id);
        this.botonNombre.setText(colectivo.getNombre());
        this.botonDescr3.setText(colectivo.getDescripcion());
        String fich="";
        int i=0;
        while(i<colectivo.getFich().size()){
            fich+= colectivo.getFich().get(i).getNombre()+'\n';
            i++;
        }
        this.botonfich.setText(fich);
        
        
        
    }
}
